package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicTranCodeDao;
import com.aidong.dao.wills.PayTypeCodeDao;
import com.aidong.model.wills.PayTypeCode;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"payType"})
public class PayTypeController extends BaseController {
    @Resource
    PayTypeCodeDao payTypeCodeDao;
    @Resource
    DicTranCodeDao dicTranCodeDao;

    public PayTypeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("dic/paytype/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.payTypeCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("dic/paytype/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PayTypeCode payType = new PayTypeCode();
        payType.setRequireNote(Integer.valueOf(0));
        payType.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("dic/paytype/edit");
        mv.addObject("payType", payType);
        mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("09"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("payType") PayTypeCode payType) {
        if(this.chkNull(payType.getCode())) {
            return this.getResult(false, "请输入编码");
        } else if(this.chkNull(payType.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(payType.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            if(this.chkNull(payType.getRequireNote())) {
                payType.setRequireNote(Integer.valueOf(0));
            }

            if(this.chkNull(payType.getStatus())) {
                payType.setStatus(Integer.valueOf(1));
            }

            if(this.payTypeCodeDao.exists(payType.getCode())) {
                return this.getResult(false, "编码已存在");
            } else {
                payType.setCreateUser(this.getLoginUserCode());
                payType.setCreateTime(new Date());
                payType.setUpdateTime(new Date());

                try {
                    this.payTypeCodeDao.add(payType);
                    this.reloadTypeList("payType");
                    this.addUserLog("新增支付类型", "编号:" + payType.getCode() + ",名称:" + payType.getChName());
                } catch (Exception var3) {
                    this.log.error("add paytype", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        PayTypeCode payType = (PayTypeCode)this.payTypeCodeDao.getById(code);
        if(payType == null) {
            return this.showAlert("没有这个支付类型");
        } else {
            ModelAndView mv = new ModelAndView("dic/paytype/edit");
            mv.addObject("payType", payType);
            mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("09"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("payType") PayTypeCode payType) {
        if(this.chkNull(payType.getCode())) {
            return this.getResult(false, "没有这个支付类型");
        } else if(this.chkNull(payType.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(payType.getSort())) {
            return this.getResult(false, "请输入排序号");
        } else {
            if(this.chkNull(payType.getRequireNote())) {
                payType.setRequireNote(Integer.valueOf(0));
            }

            if(this.chkNull(payType.getStatus())) {
                payType.setStatus(Integer.valueOf(1));
            }

            PayTypeCode payType1 = (PayTypeCode)this.payTypeCodeDao.getById(payType.getCode());
            if(payType1 == null) {
                return this.getResult(false, "没有这个支付类型");
            } else {
                payType1.setChName(payType.getChName());
                payType1.setRequireNote(payType.getRequireNote());
                payType1.setTraCode(payType.getTraCode());
                payType1.setNote(payType.getNote());
                payType1.setStatus(payType.getStatus());
                payType1.setSort(payType.getSort());
                payType1.setUpdateTime(new Date());

                try {
                    this.payTypeCodeDao.update(payType1);
                    this.reloadTypeList("payType");
                    this.addUserLog("修改支付类型", "编号:" + payType1.getCode() + ",名称:" + payType1.getChName());
                } catch (Exception var4) {
                    this.log.error("edit paytype", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        PayTypeCode payType = (PayTypeCode)this.payTypeCodeDao.getById(code);
        if(payType == null) {
            return this.getResult(false, "没有这个支付类型");
        } else {
            try {
                this.payTypeCodeDao.updateStatus(code, 0);
                this.reloadTypeList("payType");
                this.addUserLog("删除支付类型", "编号:" + payType.getCode() + ",名称:" + payType.getChName());
            } catch (Exception var4) {
                this.log.error("del paytype", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}